﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.BackstageManagement.Views;
using MyVoteMVC.Service;
using MyVoteMVC.Common;
using MyVoteMVC.Domain;
using System.IO;
using MyVoteMVC.BackstageManagement.Extensions;
using MyVoteMVC.ExcelObject;

namespace MyVoteMVC.BackstageManagement.Controllers
{
    public interface IActivityManagementController
    {
        void GetActivities();
        void GetQuestions(string activityID);
        //获取投票项目信息
        void GetItems(string questionID);
        //删除活动
        bool DeleteActivity(string activityID);
        //删除
        bool DeleteQuestion(string activityID, string questionID);
        //删除
        bool DeleteItem(string questionID, string itemID);

        void ExportToXls(string activityID);

    }

    public class ActivityManagementController : IActivityManagementController
    {
        //private IActivityService activityService;
        //private IQuestionService questionService;
        //private IItemService itemService;

        private IActivityManagementView view;
        public ActivityManagementController(IActivityManagementView view)
        {
            this.view = view;
            //activityService = Factory.GetInstance<IActivityService>();
            //questionService = Factory.GetInstance<IQuestionService>();
            //itemService = Factory.GetInstance<IItemService>();
        }

        #region activity
        public void GetActivities()
        {
            using (IActivityService activityService = Factory.GetInstance<IActivityService>())
                view.GetActivitysComplete(activityService.GetActivities());
        }

        public bool DeleteActivity(string activityID)
        {
            using (IActivityService activityService = Factory.GetInstance<IActivityService>())
            {
                activityService.DeleteActivity(activityID);
                view.DeleteActivityComplete("删除成功");
               
            }
            return true;
        }
        #endregion

        #region question
        public bool DeleteQuestion(string activityID, string questionID)
        {
            using (IQuestionService questionService = Factory.GetInstance<IQuestionService>())
            {
                questionService.DeleteQuestion(activityID, questionID);
                view.DeleteQuestionComplete("删除成功", activityID);
                
            }
            return true;
        }

        public void GetQuestions(string activityID)
        {
            using (IQuestionService questionService = Factory.GetInstance<IQuestionService>())
                view.GetQuestionsComplete(questionService.GetQuestions(activityID));
        }
        #endregion

        #region item
        public bool DeleteItem(string questionID, string itemID)
        {
            using (IItemService itemService = Factory.GetInstance<IItemService>())
            {
                itemService.DeleteItems(questionID, itemID);
                view.DeleteItemComplete("删除成功", questionID);
            }
            return true;
        }

        public void GetItems(string questionID)
        {
            using (IItemService itemService = Factory.GetInstance<IItemService>())
                view.GetItemsComplete(itemService.GetItemsList(questionID));
        }
        #endregion


        public void ExportToXls(string activityID)
        {
            using (IItemService itemService = Factory.GetInstance<IItemService>())
            {
                IList<IExcelItem> list = itemService.GetExcelItems(activityID);
                if (list != null && list.Count > 0)
                {
                    ExcelHelper helper = new ExcelHelper();
                    helper.Write(list.AsQueryable());
                }
            }
        }
    }
}
